<template>
  <view>
    <view :class="options.length > 3 ? 'container' : ''"></view>
    <view :class="options.length === 3 ? 'container-small' : ''">
      <view class="tabbar" :class="options.length === 3 ? 'tabbar-small' : ''">
        <view
          class="box"
          :class="[
            options.length === 3 ? 'tabbar-small' : '',
            needLeft ? 'tabbar-translate-left' : '',
            needRight ? 'tabbar-translate-right' : '',
          ]"
        >
          <view
            v-for="(item, index) in options"
            :key="index"
            class="tabbar-item"
            :class="[
              current === index ? 'active' : '',
              options.length > 5 ? 'tabbar-item-more' : '',
            ]"
            @tap="handleTabbarClick"
            :data-index="index"
          >
            {{ options[index] }}
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "TabBar",

  data() {
    return {
      needLeft: false,
      needRight: false,
    };
  },

  props: {
    options: {
      type: Array,
      default: () => [],
    },
    current: {
      type: Number,
      default: 0,
    },
  },

  watch: {
    current(newVal, oldVal) {
      const { options } = this;
      if (options.length > 5) {
        console.log("检测", newVal, oldVal);
        if (newVal > oldVal && (newVal == 3 || newVal == 4)) {
          this.needLeft = true;
          this.needRight = false;
          console.log("该左滑");
        } else if (
          newVal < oldVal &&
          (newVal == options.length - 4 || newVal == 2)
        ) {
          console.log("该右滑了");
          this.needRight = true;
          this.needLeft = false;
        }
      }
    },
  },

  methods: {
    handleTabbarClick(event) {
      const index = event.currentTarget.dataset.index;
      this.current = index;
      this.$emit("change", {
        current: index,
      });
    },
  },
};
</script>

<style>
.tabbar {
  width: 686rpx;
  display: flex;
  justify-content: space-between;
  font-family: "PingFang SC";
  font-style: normal;
  font-weight: 400;
  font-size: 30rpx;
  line-height: 42rpx;
  text-align: center;
  overflow: auto;
}

.container-small {
  width: 100vw;
  height: 65rpx;
  border-bottom: 1rpx solid rgba(22, 24, 35, 0.12);
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
}

.box {
  display: flex;
  justify-content: space-between;
}

/* 左滑 */
.tabbar-translate-left {
  transform: translateX(-514rpx);
  transition: all 0.5s linear;
}

/* 右滑 */
.tabbar-translate-right {
  position: relative;
  transform-origin: 0 0;
  transform: translateX(0rpx);
}

.tabbar-small {
  width: 686rpx;
  display: flex;
  justify-content: space-around;
}

.tabbar-item {
  width: auto;
  min-width: 68rpx;
  font-family: "PingFang SC";
  font-weight: 400;
  font-size: 30rpx;
  line-height: 42rpx;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  margin-right: 48rpx;
  white-space: nowrap;
}

.active {
  font-weight: 500;
  font-size: 30rpx;
  line-height: 42rpx;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  padding-bottom: 20rpx;
  border-bottom: 4rpx solid #ccb07a;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
}

/* 隐藏滚动条 */
::-webkit-scrollbar {
  display: none;
}
</style> 